<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Db;
use addons\wxsaas\library\Wechat;
use app\admin\model\Setting as SettingModel;
use app\admin\model\UserDetail as UserDetailModel;

/**
 * 认证申请管理
 */
class Apply extends Backend
{

    protected $relationSearch = true;
    protected $searchFields = 'id,applet_id,user_id';
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Apply');
    }

    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
	    $details = UserDetailModel::getUserDetail($row->user_id);
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
	        // 开启事务
	        Db::startTrans();
	        try {
	            //如果是注册小程序或者平台入驻
	            if($params['status']==20){
	                //提交到微信审核
	                $wx = new Wechat;
	                $result = $wx->fastRegisterWeApp($params['details']);
	                if($result['errcode']!=0){
	                    $this->error('提交到微信失败！code：'.$result['errcode'].'，msg：'.$result['errmsg']);
	                }
	            }
	            //如果审核通过
	            if($params['status']==30){
	                //如果是支付申请
	                if($row->apply_type==20){
	                    if(empty($params['mchid'])){
	                        $this->error('商户号不可为空');
	                    }

	                    if(!$payment = SettingModel::getItem('payment',$row->applet_id,10)){
	                        $payment = new SettingModel;
	                    }
	                    $payment->save([
	                    	'setting_type' => 10,
	                        'key' => 'payment',
	                        'describe' => '小程序支付设置',
	                        'values' => [
	                            'is_sub' => 1, //是否为特约商户
                                'mchid' => $params['mchid'],//商户号
                                'apikey' => '',//密钥
                                'cert_pem' => '', //apiclient_cert.pem 证书
                                'key_pem' => '' //apiclient_key.pem 密钥
	                        ],
	                        'applet_id' => $row->applet_id
	                    ]);
	                }
	                //如果是代理认证
	                if($row->apply_type==40){
	                    
	                }
	            }
	            //如果被驳回
	            if($params['status']==40){
	                if(empty($params['reject'])){
	                    $this->error('驳回原因不可为空');
	                }
	            }
	            $a = $row->allowField(true)->save($params);
	            // 更新资料
		        $a = $details->save($params['details']) !== false;
	            Db::commit();
	        } catch (Exception $e) {
	            Db::rollback();
	            $this->error('操作失败！');
	        }
	        $this->success();
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

}
